package juc.juccore.future;

import java.util.Random;
import java.util.concurrent.*;

/**
 * 演示FutureTask的用法
 */
public class FutureTaskDemo {
    public static void main(String[] args) {
        Task task = new Task();
        FutureTask<Integer> futureTask = new FutureTask<>(new Task());
//        new Thread(futureTask).start();
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.submit(futureTask);
        try {
            System.out.println("运行的结果:" + futureTask.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

    }
}

class Task implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        System.out.println("子线程正在计算");
        TimeUnit.SECONDS.sleep(2);
        return new Random().nextInt(100);
    }
}